package com.spa.application.dto.login;

import com.spa.domain.entity.Massagist;
import com.spa.domain.entity.ShopUser;
import com.spa.domain.entity.SysUserEntity;
import com.spa.domain.entity.UserInfo;
import com.spa.infrastructure.enums.dict.base.RequestSourceEnum;
import com.spa.infrastructure.util.IpUtils;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.Data;

import java.io.Serializable;
import java.util.Set;

/**
 * 登录用户身份权限
 * 
 * @author spa
 */
@Data
public class LoginUser implements Serializable {

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 扩展用户ID
     */
    private Long extUserId;

    /**
     * 三方openId
     */
    private String openId;

    /**
     * 商铺ID
     */
    private Long shopId;

    /**
     * 用户唯一标识
     */
    private String token;

    /**
     * 登录时间
     */
    private Long loginTime;

    /**
     * 过期时间
     */
    private Long expireTime;

    /**
     * 登录IP地址
     */
    private String ipaddr;

    /**
     * 浏览器类型
     */
    private String browser;

    /**
     * 操作系统
     */
    private String os;

    /**
     * 权限列表
     */
    private Set<String> permissions;

    /**
     * 用户端信息
     */
    private UserInfo userInfo;
    /**
     * 门店端信息
     */
    private ShopUser shopUser;
    /**
     * 技师端信息
     */
    private Massagist massagist;
    /**
     * 系统用户信息
     */
    private SysUserEntity sysUser;

    /**
     * 来源
     */
    private String source;

    public LoginUser(){
    }

    public LoginUser(Long userId, UserInfo memberUser , ShopUser shopUser , Massagist massagistUser, SysUserEntity adminUser, UserAgent userAgent, RequestSourceEnum rs) {
        this.userId = userId;
        this.userInfo = memberUser;
        this.shopUser = shopUser;
        this.massagist = massagistUser;
        this.sysUser = adminUser;
        this.source = rs.getDesc();

        String ip = IpUtils.getIpAddr();
        this.ipaddr = ip;
        // 获取客户端浏览器
        this.browser = userAgent.getBrowser().getName();
        // 获取客户端操作系统
        this.os = userAgent.getOperatingSystem().getName();
        this.loginTime = System.currentTimeMillis();
    }
    public LoginUser(LoginUser loginUser, UserAgent userAgent, RequestSourceEnum rs) {
        this.userId = loginUser.getUserId();
        this.extUserId = loginUser.getUserId();
        this.userInfo = loginUser.getUserInfo();
        this.shopUser = loginUser.getShopUser();
        this.massagist = loginUser.getMassagist();
        this.sysUser = loginUser.getSysUser();
        this.source = rs.getDesc();

        String ip = IpUtils.getIpAddr();
        this.ipaddr = ip;
        // 获取客户端浏览器
        this.browser = userAgent.getBrowser().getName();
        // 获取客户端操作系统
        this.os = userAgent.getOperatingSystem().getName();
        this.loginTime = System.currentTimeMillis();
    }

}
